package com.rongji.cms.tools.flowable.controller;/**
 * @author yang
 * @date 2022/9/20 13:26
 * @description
 */


import com.rongji.cms.tools.common.core.domain.PageQuery;
import com.rongji.cms.tools.common.core.page.TableDataInfo;
import com.rongji.cms.tools.flowable.domain.vo.WfDefinitionVo;
import com.rongji.cms.tools.flowable.service.IWfDefinitionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yang
 * @date 2022/9/20 13:26
 * @description 工作流流程定义
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/definition")
public class WfDefinitionController {

    private final IWfDefinitionService flowDefinitionService;

    @GetMapping(value = "/list")
    public TableDataInfo<WfDefinitionVo> list(PageQuery pageQuery) {
        return flowDefinitionService.list(pageQuery);
    }


    /**
     * (value = "导入流程文件", notes = "上传bpmn20的xml文件")
     *
     * @param fileName
     * @param file
     * @return
     */
    @PostMapping("/import")
    public String importFile(
            @RequestParam String fileName,
            @RequestParam("file") MultipartFile file) {
        try (InputStream in = file.getInputStream()) {
            flowDefinitionService.importFile(fileName, in);
        } catch (Exception e) {
            log.error("导入失败:", e);
            return "导入失败";
        }

        return "导入成功";
    }


    /**
     * 读取xml文件
     *
     * @param deployId 流程定义id
     * @return
     */
    @GetMapping("/readXml/{deployId}")
    public String readXml(@PathVariable(value = "deployId") String deployId) {
        try {
            return flowDefinitionService.readXml(deployId);
        } catch (Exception e) {
            return "加载xml文件异常";
        }

    }

    /**
     * 读取图片文件
     *
     * @param definitionId 流程定义id
     * @param response
     */
    @GetMapping("/readImage/{definitionId}")
    public void readImage(@PathVariable(value = "definitionId") String definitionId,
                          HttpServletResponse response) {
        try (OutputStream os = response.getOutputStream()) {
            BufferedImage image = ImageIO.read(flowDefinitionService.readImage(definitionId));
            response.setContentType("image/png");
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @ApiOperation(value = "读取图片文件")
//    @GetMapping("/readImage/{deployId}")
//    public void readImage(@ApiParam(value = "流程定义id") @PathVariable(value = "deployId") String deployId, HttpServletResponse response) {
//        OutputStream os = null;
//        BufferedImage image = null;
//        try {
//            image = ImageIO.read(flowDefinitionService.readImage(deployId));
//            response.setContentType("image/png");
//            os = response.getOutputStream();
//            if (image != null) {
//                ImageIO.write(image, "png", os);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (os != null) {
//                    os.flush();
//                    os.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }


    /**
     * 删除流程
     *
     * @param deployIds
     * @return
     */
    @PostMapping(value = "/delete")
    public String delete(@RequestParam String deployIds) {

        String[] deployIdArr = deployIds.split(",");
        for (String deployId : deployIdArr) {
            flowDefinitionService.delete(deployId);
        }


        return "删除成功";
    }


    /**
     * 根据流程定义id启动流程实例
     *
     * @param params 变量集合
     * @return
     */
    @PostMapping("/start")
    public String start(
            @RequestParam Map<String, Object> params) {
        String procDefId = params.get("procDefId").toString();
        Map<String, Object> variables = new HashMap<>();
//        移除procDefId的参数都是variables
        params.remove("procDefId");
        variables = params;
        return flowDefinitionService.startProcessInstanceById(procDefId, variables);

    }


    /**
     * 激活或挂起流程定义
     *
     * @param state    1:激活,2:挂起
     * @param deployId 流程部署ID
     * @return
     */
    @PutMapping(value = "/updateState")
    public String updateState(@RequestParam Integer state,
                              @RequestParam String deployId) {
        flowDefinitionService.updateState(state, deployId);
        return "操作成功";
    }

}
